logdir = "/opt/nginx/logs/hack"

function reidsConnent()
    redis = require("redis")
    client = redis.connect('127.0.0.1', 6379, 3)
    response_redis = client:ping()
end

function redisClose()
    client:quit()
end

function getConfig()
    reidsConnent()
    return client:hgetall("waf_config")

end

function getBlackFileExt()
    reidsConnent()
    return client:smembers("black_fileExt")
end

function getIpWhitelist()
    reidsConnent()
    local result = {}
    local ip_table = client:smembers("ipWhitelist")
    if ip_table ~= nil then
        for _, v in ipairs(ip_table) do
            local ipWhitelist = client:get("ipWhitelist:"..v)
            if ipWhitelist  then
                table.insert(result, v)
            else
                client:srem("ipWhitelist",v)
            end
        end
    end
    return result
end

function getIpBlocklist()
    reidsConnent()
    local result = {}
    local ip_table = client:smembers("ipBlocklist")
    if ip_table ~= nil then
        for _, v in ipairs(ip_table) do
            local ipBlocklist = client:get("ipBlocklist:"..v)
            if ipBlocklist  then
                table.insert(result, v)
            else
                client:srem("ipBlocklist",v)
            end
        end
    end
    return result
end

function getRegular(var)
    reidsConnent()
    return client:smembers("rule_" .. var)

end

function inputRule(table, name)
    reidsConnent()
    for _, v in pairs(table) do
        client:sadd("rule_" .. name, v)
    end

end
function inputLog(logs)
    reidsConnent()
    client:zadd("logs", os.time(), logs)
end
